var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var YjxBaseModel = require('./YjxBaseModel');
var CONFIG = require('../../config');


var YjxUserLikeModel = function (obj) {
    YjxBaseModel.call(this,obj);


//    CREATE TABLE `yjx_user_like` (
//        `like_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '评价id',
//        `type` int(3) NOT NULL DEFAULT '10' COMMENT '评价类型: 10点赞, 11踩, 20评论, 30(待定)',
//        `obj_type` int(3) NOT NULL DEFAULT '10' COMMENT '评价对象: 10活动作品, 20(待定)',
//        `obj_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '评价对象id: 10(yjx_act_user_product)',
//        `user_id` bigint(20) NOT NULL COMMENT '评价者id',
//        `data1` varchar(1000) NOT NULL DEFAULT '' COMMENT '数据1(评论用)',
//        `data2` varchar(1000) NOT NULL DEFAULT '' COMMENT '数据2',
//        `data3` varchar(1000) NOT NULL DEFAULT '' COMMENT '数据3',
//        `state` int(3) NOT NULL DEFAULT '10' COMMENT '状态: 10有效, 20无效',
//        `desc` varchar(500) DEFAULT '' COMMENT '描述',
//        `create_time` datetime NOT NULL,
//        `update_time` datetime NOT NULL,
//        `update_by` varchar(50) DEFAULT NULL,
//        `del_flg` int(1) NOT NULL DEFAULT '0',
//        PRIMARY KEY (`like_id`),
//        KEY `IDX_YJX_USR_LIKE_01` (`obj_type`,`obj_id`)
//) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='用户评价表'

};

YjxUserLikeModel.prototype = new YjxBaseModel();

// 取得表名
YjxUserLikeModel.prototype.getTableName = function(){
    return 'yjx_user_like';
};
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
YjxUserLikeModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'like_id='+this.like_id;
};

/**
 * 检测作品是否被点赞
 * @author 毛凯
 * @date   2018-7-31
 *
 * @param {Object}  param 筛选活动的参数列表
 * @param {Number}  param.productId   作品id
 * @param {Number} param.userId   用户id
 *
 * @param callback 回调
 *                err:错误信息
 *                data:返回数据
 * */

YjxUserLikeModel.prototype.checkUserProductLikeCnt = function(param,callback){
    pool.pool.getConnection(function(getConnectErr,connection) {
        if (getConnectErr) {
            console.log('YjxUserModel.getThirdPartyUserInfo getConnectErr: ' + getConnectErr);
            return callback(getConnectErr, null);
        }

        var sql="	select	" +
            "	  count(0) as `count`	" +
            "	from	" +
            "	  yjx.yjx_user_like yul	" +
            "	where yul.del_flg = 0	" +
            "	and yul.state = 10	" +
            "	and yul.obj_id = "+param.productId+"	" +
            "	and yul.user_id = "+param.userId+"	" ;
            console.log(sql);
        connection.query(sql, function (error, results) {
            connection.release();
            if (error) {
                console.log('YjxUserModel.getThirdPartyUserInfo Err: ' + error);
                return callback(error, null);
            }
            if (!results || results.length<=0){
                return callback(null,0);
            } else{
                return callback(error, results[0].count);
            }
        })
    })
};



module.exports = YjxUserLikeModel;

